package com.selfdriving.wl.activity;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.selfdriving.BaseApplication;
import com.selfdriving.BaseFragment;
import com.selfdriving.R;
import com.selfdriving.adapter.NearByPeopleAdapter;
import com.selfdriving.bean.JsonResolveUtils;
import com.selfdriving.bean.NearByPeople;
import com.selfdriving.view.RefreshExpandableListView;
import com.selfdriving.view.RefreshExpandableListView.OnCancelListener;
import com.selfdriving.view.RefreshExpandableListView.OnRefreshListener;

@SuppressLint("ValidFragment")
public class MyFriendsFragment extends BaseFragment implements
		OnRefreshListener, OnCancelListener
{

	private RefreshExpandableListView mMmrlvList;
	private NearByPeopleAdapter mAdapter;
	BaseApplication application;
	Activity activity;
	Context context;

	public MyFriendsFragment()
	{
		super();
	}

	public MyFriendsFragment(BaseApplication application, Activity activity,
			Context context)
	{
		super(application, activity, context);
		this.activity = activity;
		this.application = application;
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		mView = inflater.inflate(R.layout.w_fragment_nearbypeople, container,
				false);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void initViews()
	{
		mMmrlvList = (RefreshExpandableListView) findViewById(R.id.nearby_people_mmrlv_list);
	}

	@Override
	protected void initEvents()
	{
		mMmrlvList.setOnRefreshListener(this);
		mMmrlvList.setOnCancelListener(this);
		mMmrlvList.setOnChildClickListener(new OnChildClickListenerImpl());
	}

	private class OnChildClickListenerImpl implements OnChildClickListener
	{
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id)
		{
			Map<String, Object> map = mApplication.myFriends.get(groupPosition);
			List<NearByPeople> mNearByPeoples = (List) map.get("item");
			NearByPeople nearByPeople = mNearByPeoples.get(childPosition);
			String uid = null;
			String name = null;
			String avatar = null;
			if (childPosition > 3)
			{
				uid = "momo_p_other";
			} else
			{
				uid = nearByPeople.getUid();
			}
			name = nearByPeople.getName();
			avatar = nearByPeople.getAvatar();
			Intent intent = new Intent(mContext, OtherProfileActivity.class);
			intent.putExtra("uid", uid);
			intent.putExtra("name", name);
			intent.putExtra("avatar", avatar);
			intent.putExtra("entity_people", nearByPeople);
			startActivity(intent);
			return false;
		}

	}

	@Override
	protected void init()
	{
		getPeoples(true);
	}

	private void getPeoples(final boolean showText)
	{

		putAsyncTask(new AsyncTask<Void, Void, Boolean>()
		{

			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
				if (showText)
				{
					showLoadingDialog("正在加载,请稍后...");
				}
			}

			@Override
			protected Boolean doInBackground(Void... params)
			{
				return JsonResolveUtils.resolveMyFriends(mApplication);
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				dismissLoadingDialog();
				if (!result && showText)
				{
					showCustomToast("数据加载失败...");
				} else
				{

					mAdapter = new NearByPeopleAdapter(mMmrlvList,
							mApplication, mContext, mApplication.myFriends);
					mMmrlvList.setAdapter(mAdapter);

				}
			}
		});
	}

	@Override
	public void onCancel()
	{
		clearAsyncTask();
		mMmrlvList.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		putAsyncTask(new AsyncTask<Void, Void, Boolean>()
		{

			@Override
			protected Boolean doInBackground(Void... params)
			{
				try
				{
					Thread.sleep(1500);
					getPeoples(false);
				} catch (InterruptedException e)
				{

				}
				return null;
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				mMmrlvList.onRefreshComplete();
			}
		});
	}

	public void onManualRefresh()
	{
		mMmrlvList.onManualRefresh();
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub

	}
}
